package com.esm.sm800.sm800floating;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogFileManager {
    private static final String LOG_FILE_NAME = "app_log.txt";
    private static final String LOG_FILE_DIR = "Logs";

    public static void writeLog(Context context, String log) {
        File logFile = getLogFile(context);
        if (logFile != null) {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true))) {
                writer.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()) + " - " + log + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static File getLogFile(Context context) {
        File logFileDir = new File(Environment.getExternalStorageDirectory(), LOG_FILE_DIR);
        if (!logFileDir.exists()) {
            logFileDir.mkdirs();
        }
        File logFile = new File(logFileDir, LOG_FILE_NAME);
        return logFile;
    }
}
